package com.yksj.healthtalk.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yksj.healthtalk.entity.SalonInComeEntity;
import com.yksj.healthtalk.ui.R;

public class SalonInComeAdapter extends BaseAdapter{
	
	private Context context;
	private ArrayList<SalonInComeEntity> entities;
	private String dayTicketCountFormat;
	private String monthTicketCountFormat;
	private String fileCountFormat;
	private String voiceCountFormat;
	private String picCountFormat;
	private TreeMap<SalonInComeEntity, ArrayList<SalonInComeEntity>> map;//key主要是记录时间戳和date
	private Iterator<SalonInComeEntity> iter;
	private OnClickListener clickListener;
	private Map<Integer, SalonInComeEntity>cacheMap;
	public SalonInComeAdapter(Context context ,TreeMap<SalonInComeEntity, ArrayList<SalonInComeEntity>> map,OnClickListener clickListener) {
		super();
		this.context = context;
		this.map = map;
		this.clickListener = clickListener;
		voiceCountFormat = context.getResources().getString(R.string.voice);
		dayTicketCountFormat = context.getResources().getString(R.string.day_ticket);
		monthTicketCountFormat = context.getResources().getString(R.string.month_ticket);
		fileCountFormat = context.getResources().getString(R.string.flie);
		picCountFormat = context.getResources().getString(R.string.pic);
		cacheMap = new HashMap<Integer, SalonInComeEntity>();
		iter= map.keySet().iterator();
//		String sAgeFormat = getResources().getString(R.string.old);    
//		String sFinalAge = String.format(sAgeFormat, 23);  
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (map != null) {
			return map.size();
		}else {
			return 0;
		}
		
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}
	

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if(convertView==null){
			holder=new ViewHolder();
			convertView=LayoutInflater.from(context).inflate(R.layout.income_item_layout, null);
			holder.listLinearLayout = (LinearLayout)convertView.findViewById(R.id.list_lin);
			holder.dayTicketLinearLayout=(LinearLayout) convertView.findViewById(R.id.day_ticket_lin);
			holder.monthTicketLinearLayout=(LinearLayout) convertView.findViewById(R.id.month_ticket_lin);
			holder.pictureLinearLayout=(LinearLayout) convertView.findViewById(R.id.pic_lin);
			holder.videoLinearLayout=(LinearLayout) convertView.findViewById(R.id.video_lin);
			holder.voiceLinearLayout=(LinearLayout) convertView.findViewById(R.id.voice_lin);
			holder.day_ticket_count=(TextView) convertView.findViewById(R.id.day_ticket_count);
			holder.month_ticket_count=(TextView) convertView.findViewById(R.id.month_ticket_count);
			holder.video_count=(TextView)convertView.findViewById(R.id.video_count);
			holder.voice_count=(TextView)convertView.findViewById(R.id.voice_count);
			holder.pic_count =(TextView)convertView.findViewById(R.id.pic_count);
			holder.voice_money=(TextView)convertView.findViewById(R.id.voice_money);
			holder.video_money=(TextView)convertView.findViewById(R.id.video_money);
			holder.day_ticket_money=(TextView)convertView.findViewById(R.id.day_ticket_money);
			holder.month_ticket_money=(TextView)convertView.findViewById(R.id.month_ticket_money);
			holder.pic_money = (TextView)convertView.findViewById(R.id.pic_money);
			holder.month_time=(TextView)convertView.findViewById(R.id.month_time);
			holder.day_time=(TextView)convertView.findViewById(R.id.day_time);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
//		entities.get(position).get
//		holder.ticket_count.setText(text);
		SalonInComeEntity entity = null;
		ArrayList<SalonInComeEntity> entities;
		if (cacheMap.get(position) == null) {
			if (iter.hasNext()) {
				entity = iter.next();
				cacheMap.put(position, entity);
			}
		}
	
		
		holder.day_time.setText(cacheMap.get(position).getDate());
		if (cacheMap.get(position).getTimeStamp().equals("")) {
			holder.month_time.setVisibility(View.INVISIBLE);
		}else {
			holder.month_time.setVisibility(View.VISIBLE);
			holder.month_time.setText(cacheMap.get(position).getDate());
		}
	
		//列表点击
		holder.listLinearLayout.setOnClickListener(clickListener);
		holder.listLinearLayout.setTag(cacheMap.get(position).getDate());
		//门票 文件 语音 隐藏与否
		if ((entities = map.get(cacheMap.get(position))) != null) {
			//遍历所有日期
			holder.videoLinearLayout.setVisibility(View.GONE);
			holder.dayTicketLinearLayout.setVisibility(View.GONE);
			holder.monthTicketLinearLayout.setVisibility(View.GONE);
			holder.voiceLinearLayout.setVisibility(View.GONE);
			holder.pictureLinearLayout.setVisibility(View.GONE);
			for (int i = 0; i < entities.size(); i++) {
					if (entities.get(i).getInfo().contains(fileCountFormat)) {
						holder.videoLinearLayout.setVisibility(View.VISIBLE);
						holder.video_count.setText(entities.get(i).getInfo());
						holder.video_money.setText(entities.get(i).getChangeNum());
					}
				
					if (entities.get(i).getInfo().contains(dayTicketCountFormat)) {
						holder.dayTicketLinearLayout.setVisibility(View.VISIBLE);
						holder.day_ticket_count.setText(entities.get(i).getInfo());
						holder.day_ticket_money.setText(entities.get(i).getChangeNum());
					}
					
					if (entities.get(i).getInfo().contains(monthTicketCountFormat)) {
						holder.monthTicketLinearLayout.setVisibility(View.VISIBLE);
						holder.month_ticket_count.setText(entities.get(i).getInfo());
						holder.month_ticket_money.setText(entities.get(i).getChangeNum());
					}
				
					if (entities.get(i).getInfo().contains(voiceCountFormat)) {
						holder.voiceLinearLayout.setVisibility(View.VISIBLE);
						holder.voice_count.setText(entities.get(i).getInfo());
						holder.voice_money.setText(entities.get(i).getChangeNum());
					}
				
					if (entities.get(i).getInfo().contains(picCountFormat)) {
						holder.pictureLinearLayout.setVisibility(View.VISIBLE);
						holder.pic_count.setText(entities.get(i).getInfo());
						holder.pic_money.setText(entities.get(i).getChangeNum());
					}
			}
		}
		
		return convertView;
	}
	class ViewHolder {
		LinearLayout listLinearLayout;
		LinearLayout dayTicketLinearLayout;
		LinearLayout monthTicketLinearLayout;
		LinearLayout videoLinearLayout;
		LinearLayout voiceLinearLayout;
		LinearLayout pictureLinearLayout;
		TextView month_ticket_count;
		TextView day_ticket_count;
		TextView video_count;
		TextView voice_count;
		TextView pic_count;
		TextView voice_money;
		TextView video_money;
		TextView day_ticket_money;
		TextView month_ticket_money;
		TextView pic_money;
		TextView month_time;
		TextView day_time;
	}
	public void setIterator(Iterator<SalonInComeEntity> iterator) {
		this.iter =iterator;
	}
}
 
